/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.ulss.datadispatch.commons;

import cn.ac.iie.ulss.datadispatch.config.Configuration;
import java.util.HashMap;
import java.util.Map;
import org.I0Itec.zkclient.ZkClient;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    public static final String JETTY_SERVER_IP = "jettyServerIP";
    public static final String JETTY_SERVER_PORT = "jettyServerPort";
    public static final String JETTY_SERVER_THREAD_POOL_SIZE = "jettyServerThreadPoolSize";
    public static final String METADB_CLUSTER = "metaStoreDB";
    public static final String ZK_CLUSTER = "zkCluster";
    public static ZkClient zc = null;
    public static final String LOAD_CLUSTER = "loadCluster";
    public static final String PACKAGE_SIZE_RESTRAINT = "packeSizeRestraint";
    public static final String NAME_SERVER_LIST = "nameServerList";
    public static final String RM_CC_THREAD_POOL_SIZE = "rmCCThreadPoolSize";
    public static final String MSG_CACHE_SIZE = "msgCacheSize";
    public static final String MSG_DW_NUM = "msgDispatchWorkerNum";
    public static final String METRIC_ENABLE = "metricEnable";
    public static final String METRIC_QUEUE = "metricQueue";
    public static final String METRIC_MSG_CACHE_SIZE = "metricMsgCacheSize";
    public static final String METRIC_MSG_DW_NUM = "metricMsgDispatchWorkerNum";
    public static final String DEFAULT_MQ = "defaultMQ";
    public static final String JSON_NAME = "jsonName";
    public static final String DATA_NAME = "dataName";
    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    //logger
    static Logger logger = null;

    static {
        PropertyConfigurator.configure("log4j.properties");
        logger = Logger.getLogger(RuntimeEnv.class.getName());
    }

    public static boolean initialize(Configuration pConf) {

        if (pConf == null) {
            logger.error("configuration object is null");
            return false;
        }

        conf = pConf;
        
        String jsonName = conf.getString(JSON_NAME, "");
        if (jsonName.isEmpty()) {
            logger.error("parameter jettyServerIP does not exist or is not defined");
            return false;
        }
        dynamicParams.put(JSON_NAME, jsonName);
        
        String dataName = conf.getString(DATA_NAME, "");
        if (dataName.isEmpty()) {
            logger.error("parameter jettyServerIP does not exist or is not defined");
            return false;
        }
        dynamicParams.put(DATA_NAME, dataName);

        String jettyServerIP = conf.getString(JETTY_SERVER_IP, "");
        if (jettyServerIP.isEmpty()) {
            logger.error("parameter jettyServerIP does not exist or is not defined");
            return false;
        }
        dynamicParams.put(JETTY_SERVER_IP, jettyServerIP);

        try {
            int jettyServerPort = conf.getInt(JETTY_SERVER_PORT, -1);
            if (jettyServerPort < 0) {
                logger.error("parameter jettyServerPort does not exist or is not defined");
                return false;
            }
            dynamicParams.put(JETTY_SERVER_PORT, jettyServerPort);
        } catch (Exception ex) {
            logger.error("parameter jettyServerPort is invalid " + ex.getMessage(), ex);
            return false;
        }

        try {
            int jettyServerThreadPoolSize = conf.getInt(JETTY_SERVER_THREAD_POOL_SIZE, -1);
            if (jettyServerThreadPoolSize < 0) {
                logger.error("parameter jettyServerThreadPoolSize does not exist or is not defined");
                return false;
            }
            dynamicParams.put(JETTY_SERVER_THREAD_POOL_SIZE, jettyServerThreadPoolSize);
        } catch (Exception ex) {
            logger.error("parameter jettyServerThreadPoolSize is invalid " + ex.getMessage(), ex);
            return false;
        }

        String loadClusterString = conf.getString("loadCluster", "");
        if (loadClusterString.isEmpty()) {
            logger.error("parameter loadCluster does not exist or is not defined");
            return false;
        }
        dynamicParams.put(LOAD_CLUSTER, loadClusterString);

        String packeSizeRestraint = conf.getString("packeSizeRestraint", "900000");
        if (packeSizeRestraint.isEmpty()) {
            logger.error("parameter packeSizeRestraint does not exist or is not defined");
            return false;
        }
        try {
            dynamicParams.put(PACKAGE_SIZE_RESTRAINT, Integer.parseInt(packeSizeRestraint));
        } catch (Exception ex) {
            logger.error("parameter packeSizeRestraint's format is wrong for " + ex.getMessage(), ex);
            return false;
        }

        String rmCCThreadPoolSize = conf.getString(RM_CC_THREAD_POOL_SIZE, "");
        if (rmCCThreadPoolSize.isEmpty()) {
            logger.error("parameter rmCCThreadPoolSize does not exist or is not defined");
            return false;
        }
        try {
            dynamicParams.put(RM_CC_THREAD_POOL_SIZE, Integer.parseInt(rmCCThreadPoolSize));
        } catch (Exception ex) {
            logger.error("parameter rmCCThreadPoolSize's format is wrong for " + ex.getMessage(), ex);
            return false;
        }
        String defaultMQ = conf.getString(DEFAULT_MQ, "");
        if (defaultMQ.isEmpty()) {
            logger.error("parameter defaultMQ does not exist or is not defined");
            return false;
        }
        dynamicParams.put(DEFAULT_MQ, defaultMQ);

        String msgCacheSize = conf.getString(MSG_CACHE_SIZE, "");
        if (msgCacheSize.isEmpty()) {
            logger.error("parameter msgCacheSize does not exist or is not defined");
            return false;
        }
        try {
            dynamicParams.put(MSG_CACHE_SIZE, Integer.parseInt(msgCacheSize));
        } catch (Exception ex) {
            logger.error("parameter msgCacheSize's format is wrong for " + ex.getMessage(), ex);
            return false;
        }

        String msgDispatchWorkerNum = conf.getString(MSG_DW_NUM, "");
        if (msgDispatchWorkerNum.isEmpty()) {
            logger.error("parameter msgDispatchWorkerNum does not exist or is not defined");
            return false;
        }
        try {
            dynamicParams.put(MSG_DW_NUM, Integer.parseInt(msgDispatchWorkerNum));
        } catch (Exception ex) {
            logger.error("parameter msgDispatchWorkerNum's format is wrong for " + ex.getMessage(), ex);
            return false;
        }

        String metricEnable = conf.getString(METRIC_ENABLE, "");
        if (metricEnable.isEmpty()) {
            logger.error("parameter metricEnable does not exist or is not defined");
            return false;
        }
        try {
            dynamicParams.put(METRIC_ENABLE, Boolean.parseBoolean(metricEnable));
        } catch (Exception ex) {
            logger.error("parameter metricEnable's format is wrong for " + ex.getMessage(), ex);
            return false;
        }

        String metricQueue = conf.getString("metricQueue", "");
        if (metricQueue.isEmpty()) {
            logger.error("parameter metricQueue does not exist or is not defined");
            return false;
        }
        dynamicParams.put(METRIC_QUEUE, metricQueue);

        String metricMsgCacheSize = conf.getString(METRIC_MSG_CACHE_SIZE, "");
        if (metricMsgCacheSize.isEmpty()) {
            logger.error("parameter metricMsgCacheSize does not exist or is not defined");
            return false;
        }
        try {
            dynamicParams.put(METRIC_MSG_CACHE_SIZE, Integer.parseInt(metricMsgCacheSize));
        } catch (Exception ex) {
            logger.error("parameter metricMsgCacheSize's format is wrong for " + ex.getMessage(), ex);
            return false;
        }

        String metricMsgDispatchWorkerNum = conf.getString(METRIC_MSG_DW_NUM, "");
        if (metricMsgDispatchWorkerNum.isEmpty()) {
            logger.error("parameter metricMsgDispatchWorkerNum does not exist or is not defined");
            return false;
        }
        try {
            dynamicParams.put(METRIC_MSG_DW_NUM, Integer.parseInt(metricMsgDispatchWorkerNum));
        } catch (Exception ex) {
            logger.error("parameter metricMsgDispatchWorkerNum's format is wrong for " + ex.getMessage(), ex);
            return false;
        }

        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
